package com.web.controoller;

import com.web.constant.UserConstants;
import com.web.pojo.domain.Permission;
import com.web.pojo.domain.User;
import com.web.pojo.domain.UserInfo;
import com.web.pojo.domain.UserPermission;
import com.web.pojo.vo.CommonResult;
import com.web.pojo.vo.Menu;
import com.web.service.UserServer;
import com.web.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wws
 * @Date: 2021/1/12 15:52
 * describe:
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class  UserController {

    @Autowired
    UserServer userServer;

    @PreAuthorize("hasAuthority('userMinfo:insert')")
    @PostMapping("/insert")
    @ApiOperation("添加子用户")
    public CommonResult insertUserInfo(@RequestBody UserInfo userInfo) {
//        return CommonResult.success(userInfo.toString());
        if (UserConstants.NOT_UNIQUE.equals(userServer.checkUserNameUnique(userInfo.getUsername()))) {
            return CommonResult.error(UserConstants.USERNAME_NOT_UNIQUE);
        }
        userInfo.setPid(SecurityUtils.getUserId());
        userInfo.setPassword(SecurityUtils.encryptPassword(userInfo.getPassword()));
        userInfo.setCreateBy(SecurityUtils.getUsername());
        return CommonResult.insert(userServer.insertUserInfo(userInfo));
    }

    @PreAuthorize("hasAuthority('userMinfo:updata')")
    @PostMapping("/updateInfo")
    @ApiOperation("修改子用户信息")
    public CommonResult updateInfo(@RequestBody UserInfo userInfo) {
        userInfo.setUpdateBy(SecurityUtils.getUsername());
        return CommonResult.update(userServer.updateUserInfo(userInfo));
    }

    @PreAuthorize("hasAuthority('addressTree:delete')")
    @PostMapping("/delete")
    @ApiOperation("删除子用户")
    public CommonResult deleteUserInfo(@RequestBody List<Integer> userIds) {
        return CommonResult.delete(userServer.deleteUserInfo(userIds));
    }

    @PreAuthorize("hasAuthority('userMinfo:updata')")
    @PostMapping("/resetPwd")
    @ApiOperation("重置子用户密码")
    public CommonResult resetPwd(@RequestBody UserInfo userInfo) {
        userInfo.setUpdateBy(SecurityUtils.getUsername());
        userInfo.setPassword(SecurityUtils.encryptPassword(userInfo.getPassword()));
        return CommonResult.update(userServer.updateUserInfo(userInfo));
    }

    @PreAuthorize("hasAuthority('userMinfo:updata')")
    @PostMapping("/updateAuthority")
    @ApiOperation("修改子用户权限")
    public CommonResult updateAuthority(@RequestBody UserInfo userInfo) {
        return CommonResult.update(userServer.updateAuthority(userInfo));
    }
    @PreAuthorize("hasAuthority('addressTree:select')")
    @PostMapping("/list")
    @ApiOperation("查看子用户信息")
    public CommonResult list(@RequestBody(required = false) String str) {
        List<UserInfo> userList = userServer.selectUserList(str);
        return CommonResult.select(userList);
    }

    @PreAuthorize("hasAuthority('addressTree:select')")
    @PostMapping("/userInfo")
    @ApiOperation("查看子用户信息")
    public CommonResult userInfo() {
        Integer userId = SecurityUtils.getUserId();
        UserInfo userList = userServer.selectUserInfo(userId);
        return CommonResult.select(userList);
    }

    @PreAuthorize("hasAuthority('addressTree:selete')")
    @PostMapping("/authorityList")
    @ApiOperation("查看用户权限列表")
    public CommonResult authorityList(@RequestBody(required = false) Integer userId) {
        if (userId==null){
            userId=SecurityUtils.getUserId();
        }

        List<Permission> list = userServer.selectAuthorityList(userId);

        return CommonResult.select(list);
    }

    @PreAuthorize("hasAuthority('userMinfo:insert')")
    @PostMapping("/checkUserNameUnique")
    @ApiOperation("检查用户名是否注册")
    public CommonResult checkUserNameUnique(String username) {
        if (UserConstants.NOT_UNIQUE.equals(userServer.checkUserNameUnique(username))) {
            return CommonResult.error(UserConstants.USERNAME_NOT_UNIQUE);
        }
        return CommonResult.success(UserConstants.USERNAME_UNIQUE);
    }

//    FF FF F0 00 -FF FF FF FF
//    FF FF F0 00 -00 00 00 00
}
